package controller

import (
	"context"
	"net/http"

	"web/global"
	"web/proto/myorder"
	"web/utils"

	"github.com/gin-contrib/sessions"
	"github.com/micro/micro/v3/service"

	"github.com/gin-gonic/gin"
)

type OrderStu struct {
	EndDate   string `json:"end_date"`
	HouseId   string `json:"house_id"`
	StartDate string `json:"start_date"`
}

//下订单
func PostOrders(ctx *gin.Context) {
	resp := make(map[string]interface{})
	defer ctx.JSON(http.StatusOK, resp)
	//获取数据
	var order OrderStu
	err := ctx.Bind(&order)

	//校验数据
	if err != nil {
		global.GlobalLog.Error(context.Background(), "获取数据错误:", err)
		return
	}
	//获取用户名
	userName := sessions.Default(ctx).Get("userName")

	srv := service.New()

	client := myorder.NewUserOrderService("order", srv.Client())

	rsp, err := client.CreateOrder(context.Background(), &myorder.Request{
		StartDate: order.StartDate,
		EndDate:   order.EndDate,
		HouseId:   order.HouseId,
		UserName:  userName.(string),
	})
	//获取数据库数据
	if err != nil {
		global.GlobalLog.Error(context.Background(), "下订单错误:", err)
		resp["errno"] = utils.RECODE_DBERR
		resp["errmsg"] = utils.RecodeText(utils.RECODE_DBERR)
		return
	}
	//把数据返回给前端
	resp["errno"] = rsp.Errno
	resp["errmsg"] = rsp.Errmsg
	resp["data"] = rsp.Data

}

//获取订单信息
func GetUserOrder(ctx *gin.Context) {
	resp := make(map[string]interface{})
	defer ctx.JSON(http.StatusOK, resp)
	//获取get请求传参
	role := ctx.Query("role")
	//校验数据
	if role == "" {
		global.GlobalLog.Error(context.Background(), "获取数据失败")
		resp["errno"] = utils.RECODE_DBERR
		resp["errmsg"] = utils.RecodeText(utils.RECODE_DBERR)
		return
	}

	srv := service.New()

	client := myorder.NewUserOrderService("order", srv.Client())

	rsp, err := client.GetOrderInfo(context.Background(), &myorder.GetReq{
		Role:     role,
		UserName: sessions.Default(ctx).Get("userName").(string),
	})
	//获取数据库数据
	if err != nil {
		global.GlobalLog.Error(context.Background(), "获取订单信息错误:", err)
		resp["errno"] = utils.RECODE_DBERR
		resp["errmsg"] = utils.RecodeText(utils.RECODE_DBERR)
		return
	}
	//把数据返回给前端
	resp["errno"] = rsp.Errno
	resp["errmsg"] = rsp.Errmsg
	resp["data"] = rsp.Data
}

type StatusStu struct {
	Action string `json:"action"`
	Reason string `json:"reason"`
}

//更新订单状态
func PutOrders(ctx *gin.Context) {
	resp := make(map[string]interface{})
	defer ctx.JSON(http.StatusOK, resp)
	//获取数据
	id := ctx.Param("id")
	var statusStu StatusStu
	err := ctx.Bind(&statusStu)

	//校验数据
	if err != nil || id == "" {
		global.GlobalLog.Error(context.Background(), "更新订单状态错误:", err)
		resp["errno"] = utils.RECODE_DBERR
		resp["errmsg"] = utils.RecodeText(utils.RECODE_DBERR)
		return
	}
	srv := service.New()

	client := myorder.NewUserOrderService("order", srv.Client())

	rsp, err := client.UpdateStatus(context.Background(), &myorder.UpdateReq{
		Action: statusStu.Action,
		Reason: statusStu.Reason,
		Id:     id,
	})
	//获取数据库数据
	if err != nil {
		global.GlobalLog.Error(context.Background(), "更新订单状态错误:", err)
		resp["errno"] = utils.RECODE_DBERR
		resp["errmsg"] = utils.RecodeText(utils.RECODE_DBERR)
		return
	}
	//把数据返回给前端
	resp["errno"] = rsp.Errno
	resp["errmsg"] = rsp.Errmsg

}
